package de.hhbk.azubiverwaltung.dao.beans;

import java.util.List;

import javax.persistence.Query;

import de.hhbk.azubiverwaltung.dao.interfaces.AzubiDaoIF;
import de.hhbk.azubiverwaltung.entites.Azubi;

public class AzubiDAOBean extends GenericDao implements AzubiDaoIF {

	@Override
	public Azubi persistAzubi(Azubi azubi) {
		manager.getTransaction().begin();
		try
		{
			manager.persist(azubi);
			manager.getTransaction().commit();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			manager.getTransaction().rollback();
		}
		return azubi;
	}

	@Override
	public List<Azubi> persistAzubi(List<Azubi> azubi) {
		manager.getTransaction().begin();
		try
		{
			for(Azubi a : azubi)
			{
				manager.persist(a);
			}
			manager.getTransaction().commit();
		}
		catch(Exception ex)
		{
			manager.getTransaction().rollback();
		}
		
		return azubi;
	}

	@Override
	public Azubi updateAzubi(Azubi azubi) {
		manager.getTransaction().begin();
		try
		{
			manager.merge(azubi);
			manager.getTransaction().commit();
		}
		catch(Exception ex)
		{
			manager.getTransaction().rollback();
		}
		return azubi;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Azubi> holeAlleAzubis() {
		Query query = manager.createQuery("SELECT a FROM Azubi a");
		return query.getResultList();
	}

}
